package com.chen.blue.service.system.impl;

import com.chen.blue.domain.model.system.SysDictData;
import com.chen.blue.mapper.system.SysDictDataMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.security.SecurityService;
import com.chen.blue.service.system.ISysDictDataService;
import com.chen.blue.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service("dictDataService")
public class ISysDictDataServiceImpl implements ISysDictDataService {

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public DataResult<List<SysDictData>> selectDictDataList(SysDictData dictData) {

        if (Objects.nonNull(dictData.getPageNo()) && Objects.nonNull(dictData.getPageSize())) {
            PageHelper.startPage(dictData.getPageNo(), dictData.getPageSize());
        }

        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(dictData);
        PageInfo<SysDictData> pageInfo = new PageInfo<>(sysDictDataList);
        PageHelper.clearPage();

        DataResult<List<SysDictData>> dataResult = new DataResult<>();
        dataResult.setData(pageInfo.getList());
        PageInfoUtil.setPageInfo(dataResult, pageInfo);

        return dataResult;

    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data) {
        int row = dictDataMapper.insertDictData(data);
        return row;
    }


    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data) {
        int row = dictDataMapper.updateDictData(data);
        return row;
    }
}
